package com.example.easeimpro.controller.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.easeimpro.R;
import com.example.easeimpro.controller.activity.HomeLoginActivity;
import com.example.easeimpro.model.bean.Model;
import com.example.easeimpro.utils.SpUtils;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;

/**
 * 设置页面
 */
public class OptionFragment extends Fragment {

    private Button btnOut;
    private View view;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.option_main_fragment,container,false);
        initView();
     initOnClick();

        return view;
    }

    private static final String TAG = "OptionFragment";
    private void initView() {
        btnOut = view.findViewById(R.id.bt_setting_out);
        CheckBox box = view.findViewById(R.id.option_box);
        box.setChecked(SpUtils.getInstance().getFriendBox());
        box.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SpUtils.getInstance().setFriendBox(box.isChecked());
                Log.e(TAG, "onClick: "+box.isChecked() );
            }
        });
        btnOut.setText("退出登录("+EMClient.getInstance().getCurrentUser()+")");
    }

    private void initOnClick() {
        btnOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //退出登录
                Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
                    @Override
                    public void run() {
                        EMClient.getInstance().logout(true, new EMCallBack() {
                            @Override
                            public void onSuccess() {
                                //退出是关闭DbHelper
                                Model.getInstance().getDbManager().close();

                               getActivity().runOnUiThread(new Runnable() {
                                   @Override
                                   public void run() {
                                       Intent intent = new Intent(getContext(), HomeLoginActivity.class);
                                       Toast.makeText(getContext(),"退出成功",Toast.LENGTH_SHORT).show();

                                       startActivity(intent);//跳转
                                       getActivity().finish();//销毁
                                   }
                               });

                            }

                            @Override
                            public void onError(int code, String error) {
                                getActivity().runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(getContext(),"退出失败"+error,Toast.LENGTH_SHORT).show();
                                    }
                                });

                            }
                        });
                    }
                });


            }
        });

    }
}
